package com.headcaselabs.orchestra;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import com.thoughtworks.xstream.XStream;
/**
 * Helper that initializes a configuration file
 * @author Florian Leibert
 *
 */
public class ConfigurationInit {
	public static void main(String[] args) throws IOException{
		XStream xstream = new XStream();
		xstream.alias("configuration", Configuration.class);
		Configuration conf = new Configuration();
		if (args.length<7) {
			System.out.println("Usage: java ConfigurationInit file accessKey secretKey keyName default_timeout reportQueue:timeout queue_name:timeout:init_time:expected_processing_time:weight  queue_name_n:timeout:init_time:expected_processing_time:weight");
			System.out.println("Example:");
			System.out.println(" java ConfigurationInit configuration.xml YOUR_ACCESS_KEY YOUR_SECRET_KEY YOUR_KEY_NAME 20 samplequeue1:10:0:50000:1.0  samplequeue2:20:1000:4000:2.0");
			System.exit(1);
		}
		FileWriter fw = new FileWriter(new File(args[0]));
		conf.setAwsAccessKey(args[1]);
		conf.setAwsSecretKey(args[2]);
		conf.setKeyName(args[3]);
		conf.setDefaultTimeout(Integer.parseInt(args[4]));
		conf.setReportQueue(args[5].split(":")[0]);
		conf.setReportQueueTimeout(Integer.parseInt(args[5].split(":")[1]));
		for (int i = 6; i < args.length; i++ ) {
			String queueStr = args[i];
			String[] queueArr = queueStr.split(":");
			if (queueArr.length!=5) {
				System.out.println("Error, malformatted queue string. It should be in the form 'queue_name:init_time:expected_processing_time:weight'");
				System.exit(1);
			}
			conf.getQueues().add(queueArr[0]);

			conf.getTimeouts().add(Integer.parseInt(queueArr[1]));
			conf.getQueueCpuTimes().add(Long.parseLong(queueArr[2]));
			conf.getQueueExpectedProcessingTimes().add(Long.parseLong(queueArr[3]));
			conf.getQueueWeights().add(Double.parseDouble(queueArr[4]));
		}
		conf.setDefaultTimeout(30);
		conf.setWorkingTime(90000000L);
		conf.setReportInterval(1200000L);
		conf.setJobDirectory("jobdirectory");
		conf.setMaxLoad(10d);
		conf.setLoadScript("/usr/bin/get_load.sh");
		conf.setReportDirectory("reportDirectory");
		conf.setThreadCount(4);
		xstream.toXML(conf,fw);
  	fw.flush();
  	fw.close();
	}

}
